﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using MainGameModule.Models;
using YourWorld.Common;
using BusinessObjects;
using System.Collections.ObjectModel;
using YourWorld.Common.GreenWorldEntitiesService;
using System.Linq;

namespace MainGameModule.ViewModels
{
    public class QuestsPreviewViewModel : ViewModelBase
    {
        private IRegionManager regionManager;
        private IUnityContainer container;
        private IGameModel gameModel;

        private ObservableCollection<Event> pendingQuests;
        private bool areQuestsAvailable = false;

        public Action<Event> EventCompleted;

        public QuestsPreviewViewModel( IRegionManager regionManager, IUnityContainer container, IGameModel gameModel )
        {
            this.regionManager = regionManager;
            this.container = container;
            this.gameModel = gameModel;
        }

        public bool AreQuestsAvailable
        {
            get
            {
                return this.areQuestsAvailable;
            }
            set
            {
                if ( this.areQuestsAvailable == value )
                    return;

                this.areQuestsAvailable = value;
                this.NotifyPropertyChanged( "AreQuestsAvailable" );
            }
        }

        public ObservableCollection<Event> PendingQuests
        {
            get
            {
                if ( this.pendingQuests == null )
                {
                    this.pendingQuests = new ObservableCollection<Event>();
                    this.pendingQuests.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler( pendingQuests_CollectionChanged );
                }

                return this.pendingQuests;
            }
            set
            {
                if ( this.pendingQuests == value )
                    return;

                if ( this.pendingQuests != null )
                    this.pendingQuests.CollectionChanged -= new System.Collections.Specialized.NotifyCollectionChangedEventHandler( pendingQuests_CollectionChanged );

                this.pendingQuests = value;
                this.pendingQuests.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler( pendingQuests_CollectionChanged );

                this.NotifyPropertyChanged( "PendingQuests" );
            }
        }

        public void RubbishFound( Rubbish rubbish )
        {
            if ( this.AreQuestsAvailable == false )
                return;

            bool considered = false;
            Event consideredQuest = null;
            foreach ( Event q in this.PendingQuests )
            {
                Event firstQuest = q;
                foreach ( UserBadge badge in firstQuest.RequiredBadges )
                {
                    BadgeType type = BadgesRepository.Instance.BadgeTypes.Where( x => x.ID == badge.TypeId ).FirstOrDefault();

                    if ( type.TargetId == rubbish.MaterialTypeId )
                    {
                        badge.CurrentItems++;
                        if ( badge.TargetItems == badge.CurrentItems )
                            badge.IsLocked = false;

                        considered = true;
                        consideredQuest = q;
                        break;
                    }
                }

                if ( considered == true )
                    break;                
            }

            if ( consideredQuest == null )
                return;

            bool isCompleted = true;

            foreach ( UserBadge badge in consideredQuest.RequiredBadges )
            {
                if ( badge.IsLocked == true )
                {
                    isCompleted = false;
                    break;
                }
            }

            if ( isCompleted == true )
            {
                this.PendingQuests.Remove( consideredQuest );

                if ( this.EventCompleted != null )
                    this.EventCompleted( consideredQuest );
            }
        }

        private void pendingQuests_CollectionChanged( object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e )
        {
            this.AreQuestsAvailable = this.PendingQuests.Count != 0;
        }
    }
}
